package cn.ibizlab.odoo.core.odoo_product.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproduct_pricelist;
import cn.ibizlab.odoo.core.odoo_product.domain.Product_pricelist;
import cn.ibizlab.odoo.core.odoo_product.filter.Product_pricelistSearchContext;
import cn.ibizlab.odoo.core.odoo_product.service.IProduct_pricelistService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[价格表] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Product_pricelistServiceImpl implements IProduct_pricelistService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproduct_pricelistClientService product_pricelistClientService;

    @Override
    public Product_pricelist get(Integer id) {
        Iproduct_pricelist clientModel = product_pricelistClientService.createModel();
        clientModel.setId(id);
        product_pricelistClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Product_pricelist get(Product_pricelist product_pricelist) {
        Product_pricelist retproduct_pricelist = this.get(product_pricelist.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproduct_pricelist.getClass(), product_pricelist.getClass(), false);
        copier.copy(retproduct_pricelist, product_pricelist, null);
        return product_pricelist ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //product_pricelistClientService.removeBatch(product_pricelists) ;

    }

    @Override
    public boolean update(Product_pricelist product_pricelist) {
        boolean bOk = false ;
        Iproduct_pricelist clientModel = this.convert2Model(product_pricelist,null);
        product_pricelistClientService.update(clientModel) ;
        this.convert2Domain(clientModel,product_pricelist) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproduct_pricelist clientModel = product_pricelistClientService.createModel();
        clientModel.setId(id);
        product_pricelistClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Product_pricelist> product_pricelists, int batchSize) {

        //product_pricelistClientService.updateBatch(product_pricelists,batchSize) ;

    }

    @Override
    public void createBatch(List<Product_pricelist> product_pricelists, int batchSize) {

                        //product_pricelistClientService.createBatch(product_pricelists,batchSize) ;

    }

    @Override
    public boolean create(Product_pricelist product_pricelist) {
        boolean bOk = false ;
        Iproduct_pricelist clientModel = this.convert2Model(product_pricelist,null);
        product_pricelistClientService.create(clientModel) ;
        this.convert2Domain(clientModel,product_pricelist);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Product_pricelist> searchDefault(Product_pricelistSearchContext context) {
        List<Product_pricelist> list = new ArrayList<Product_pricelist>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist> clientModelList = product_pricelistClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据公司查询
     */
    @Override
    public Page<Product_pricelist> selectByOdoo_company(Integer id){
        Product_pricelistSearchContext context = new Product_pricelistSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist> list = new ArrayList<Product_pricelist>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist> clientModelList = product_pricelistClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据币种查询
     */
    @Override
    public Page<Product_pricelist> selectByOdoo_currency(Integer id){
        Product_pricelistSearchContext context = new Product_pricelistSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist> list = new ArrayList<Product_pricelist>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist> clientModelList = product_pricelistClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_pricelist> selectByOdoo_create(Integer id){
        Product_pricelistSearchContext context = new Product_pricelistSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist> list = new ArrayList<Product_pricelist>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist> clientModelList = product_pricelistClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_pricelist> selectByOdoo_write(Integer id){
        Product_pricelistSearchContext context = new Product_pricelistSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist> list = new ArrayList<Product_pricelist>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist> clientModelList = product_pricelistClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproduct_pricelist convert2Model(Product_pricelist domain , Iproduct_pricelist model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = product_pricelistClientService.createModel() ;
        }

         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getSelectableDirtyFlag())
            model.setSelectable(domain.getSelectable());
         if(domain.getDiscount_policyDirtyFlag())
            model.setDiscount_policy(domain.getDiscount_policy());
         if(domain.getCountry_group_idsDirtyFlag())
            model.setCountry_group_ids(domain.getCountry_group_ids());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getWebsite_idDirtyFlag())
            model.setWebsite_id(domain.getWebsite_id());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getItem_idsDirtyFlag())
            model.setItem_ids(domain.getItem_ids());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
        return model ;
    }

    public Product_pricelist convert2Domain( Iproduct_pricelist model ,Product_pricelist domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Product_pricelist();
        }

        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getSelectableDirtyFlag())
            domain.setSelectable(model.getSelectable());
        if(model.getDiscount_policyDirtyFlag())
            domain.setDiscount_policy(model.getDiscount_policy());
        if(model.getCountry_group_idsDirtyFlag())
            domain.setCountry_group_ids(model.getCountry_group_ids());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getWebsite_idDirtyFlag())
            domain.setWebsite_id(model.getWebsite_id());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getItem_idsDirtyFlag())
            domain.setItem_ids(model.getItem_ids());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        return domain ;
    }


}
